package com.sudo.boot.netty.reconnect;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.concurrent.EventExecutorGroup;

public class NettyReconnetClientHandler extends SimpleChannelInboundHandler<String> {

    private Bootstrap bootstrap;

    NettyReconnetClientHandler(Bootstrap bootstrap) {
        this.bootstrap = bootstrap;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
        System.out.println("收到服务端的数据：" + s);
    }

    /**
     * 掉线后会调用此方法：手动重新连接
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("服务掉线，重连中...");
        NettyClient.connect(bootstrap);
    }
}
